package com.yj.springbootmybatis.util;

import com.yj.springbootmybatis.pojo.PageInfo;
import lombok.Data;

import java.util.List;

@Data
public class PageUtils {

   public static PageInfo getPageList(Integer pageNum,Integer pageSize,List data){

       int size = data.size();
       int pageCount = size % pageSize == 0 ? size/pageSize : size/pageSize + 1;
       PageInfo pageInfo = new PageInfo();
       pageInfo.setPageCount(pageCount);
       pageInfo.setTotal(data.size());
       pageInfo.setPageNum(pageNum);
       pageInfo.setPageSize(pageSize);

       int beginIndex = 0; //开始索引
       int endIndex = 0;   //结束索引

        if (pageNum<pageCount){
            beginIndex = (pageNum-1) * pageSize;
            endIndex = beginIndex+pageSize;
        }else {
            beginIndex = (pageNum-1) * pageSize;
            endIndex = size;
        }

       List list = data.subList(beginIndex, endIndex);
       pageInfo.setData(list);
       return pageInfo;

   }
}
